const metaWait = {
    img: "https://media.example.com/wait_1.gif",
    openResult: "开门请求中",
    title: "开门结果通知",
    status: "wait"
}

const metaSuceessOpen = {
    img: "/static/icons/success.png",
    openResult: "开门成功",
    title: "开门结果通知",
    status: "successOpen"
}

const metaSuceessClose = {
    img: "/static/icons/success.png",
    openResult: "关门成功",
    title: "关门结果通知",
    status: "successClose"
}

const metaFail = {
    img: "/static/icons/fail.png",
    openResult: "开门失败",
    title: "开门结果通知",
    status: "fail"
}

const orderStatusCode = {
    success_open: [10], //开门成功
    success_close: [20, 30, 40, 50, 60, 70], //关门成功
    fail: [80, 90] //开门失败
}

export default {
    data() {
        return {
            orderId: "",
            meta: metaWait,
            timer: null
        }
    },
    onLoad(options) {

        this.orderId = options.orderId;

        if (this.orderId == null || this.orderId == "" || this.orderId == "undefined") {
            this.orderId = ""
        }

        uni.setNavigationBarTitle({
            title: this.meta.title
        });

        this.startMyListener(); //开启订单状态监听,默认监听最新的订单信息

    },
    beforeDestroy() {
        clearInterval(this.timer);
    },

    methods: {
        startMyListener: function() {
            this.timer = setInterval(this.queryOrderStatus, 1000)
        },
        queryOrderStatus: function() {

            let data = {
                orderId: this.orderId,
            }

            this.$request(this.$api.QueryOrderStatus, data, "GET", false).then(res => {
                if (res.code == 200) {

                    if (orderStatusCode.success_open.includes(res.result)) {
                        this.meta = metaSuceessOpen;

                    } else if (orderStatusCode.fail.includes(res.result)) {
                        this.meta = metaFail;
                        clearInterval(this.timer)

                    } else if (orderStatusCode.success_close.includes(res.result)) {
                        this.meta = metaSuceessClose;
                        clearInterval(this.timer)

                    } else {
                        //不处理定时器
                    }

                } else {
                    uni.showToast({
                        title: res.message,
                        icon: "none"
                    });
                }
            })

        }
    }

}